package com.heba;

import android.content.Context;
import android.content.SharedPreferences;

import com.heba.greenmulu.BuildConfig;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 配置信息
 * <p>
 * Created by shenghao on 2016/12/06.
 */
public class Cfg {

    public static int Language = 0;   //0:默认系统语言  1:英文  2:繁体中文
    public static boolean FirstSetNotification = true;    //是否首次设置通知

    public static final Integer appVersionCode = BuildConfig.VERSION_CODE;
    public static final String appVersionName = BuildConfig.VERSION_NAME;

    public static String BaseUrl =  "http://greenmulu.atomtechnology.com.hk/api";
            //BuildConfig.test_url;
    public static String PrivacyUrl = BuildConfig.SERVER_URL_PRIVACY;
    public static String TermsUseUrl = BuildConfig.SERVER_URL_TERMS;
    public static String ShareLink = "https://www.greenmulu.com";
    public static String ShareImageUrl = "http://greenmuluhk-1253933048.coshk.myqcloud.com/5e9818166d285.png";

    public static String activity = "";
    public static String prevActivity = "";

    public static SharedPreferences prefs;
    public static SharedPreferences.Editor editor;
    //用户信息
    public static String SessionKey = "";
    public static String UserID = "";
    public static String Account = "";
    public static String NickName = "";
    public static String BannerUrl = "";
    public static String Icon = "";
    public static String LastName = "";
    public static String Sex = "0";
    public static String BirthYear = "";
    public static String BirthMonth = "";
    public static String Address = "";
    public static String Addressen = "";
    public static String Email = "";
    public static String Openid = "";
    public static String UnionID = "";
    public static String AccountType = "";
    public static String FaceBookUserID = "";
    public static float AppVer = 0f;
    public static boolean isPushTime1 = true;   //true表示設置(8am-2pm)時間段允許推送
    public static boolean isPushTime2 = true;   //true表示設置(2pm-8pm)時間段允許推送
    public static int Leng = 0;
    public static int isEnergy = 0;
    public static int isProtein = 0;
    public static int isFat = 0;
    public static int isSatFat = 0;
    public static int isCarb = 0;
    public static int isSugar = 0;
    public static int isSodium = 0;

    public static String uploadImageUrl = "";

    public static boolean isNeedToReleaseCamera = false;
    public static boolean isProduct = false;

    public static void setActivity(String str) {
        prevActivity = activity;
        activity = str;
    }

    public static void setContext(Context context) {
        prefs = context.getSharedPreferences("Cfg", 0);
        editor = prefs.edit();
        get();
    }

    /**
     * 获取sharepreferences数据
     */
    public static void get() {
        SessionKey = prefs.getString("SessionKey", "");
        UserID = prefs.getString("UserID", "");
        Account = prefs.getString("Account", "");
        Email = prefs.getString("Email", "");
        Icon = prefs.getString("Icon", "");
        LastName = prefs.getString("LastName", "");
        NickName = prefs.getString("NickName", "");
        BirthYear = prefs.getString("BirthYear", "");
        Language = prefs.getInt("Language", 0);
        isPushTime1 = prefs.getBoolean("isPushTime1", true);
        isPushTime2 = prefs.getBoolean("isPushTime2", true);
        AccountType = prefs.getString(AccountType,"");
        FirstSetNotification = prefs.getBoolean("FirstSetNotification", true);
        AppVer = prefs.getFloat("AppVer", 0f);
        BannerUrl = prefs.getString("BannerUrl","");
        AccountType = prefs.getString("AccountType","");
    }

    /**
     * 获取保存的筛选记录
     */
    public static void getProductRecord() {
        if (prefs != null) {
            isEnergy = prefs.getInt("isEnergy", 0);
            isProtein = prefs.getInt("isProtein", 0);
            isFat = prefs.getInt("isFat", 0);
            isSatFat = prefs.getInt("isSatFat", 0);
            isCarb = prefs.getInt("isCarb", 0);
            isSugar = prefs.getInt("isSugar", 0);
            isSodium = prefs.getInt("isSodium", 0);
        }
    }

    public static void change(String key, Long value) {
        editor.putLong(key, value);
    }

    public static void change(String key, int value) {
        editor.putInt(key, value);
        if (key.equals("Language")) Language = value;
        if (key.equals("isEnergy")) isEnergy = value;
        if (key.equals("isProtein")) isProtein = value;
        if (key.equals("isFat")) isFat = value;
        if (key.equals("isSatFat")) isSatFat = value;
        if (key.equals("isCarb")) isCarb = value;
        if (key.equals("isSugar")) isSugar = value;
        if (key.equals("isSodium")) isSodium = value;
    }

    public static void change(String key, String value) {
        editor.putString(key, value);
        if (key.equals("SessionKey")) SessionKey = value;
        if (key.equals("UserID")) UserID = value;
        if (key.equals("Account")) Account = value;
        if (key.equals("NickName")) NickName = value;
        if (key.equals("Icon")) Icon = value;
        if (key.equals("LastName")) LastName = value;
        if (key.equals("Sex")) Sex = value;
        if (key.equals("BirthYear")) BirthYear = value;
        if (key.equals("BirthMonth")) BirthMonth = value;
        if (key.equals("Address")) Address = value;
        if (key.equals("Addressen")) Addressen = value;
        if (key.equals("Email")) Email = value;
        if (key.equals("Openid")) Openid = value;
        if (key.equals("UnionID")) UnionID = value;
        if (key.equals("FaceBookUserID")) FaceBookUserID = value;
        if (key.equals("AccountType")) AccountType = value;
    }

    public static void change(String key, boolean value) {
        editor.putBoolean(key, value);
        if (key.equals("isPushTime1")) isPushTime1 = value;
        if (key.equals("isPushTime2")) isPushTime2 = value;
        if (key.equals("FirstSetNotification")) FirstSetNotification = value;
    }

    public static void change(String key, float value) {
        editor.putFloat(key, value);
        if (key.equals("AppVer")) AppVer = value;
    }

    public static void save() {
        editor.commit();
    }

    public static void save(String key, Long value) {
        change(key, value);
        save();
    }

    public static void save(String key, String value) {
        change(key, value);
        save();
    }

    public static void save(String key, int value) {
        change(key, value);
        save();
    }

    public static void save(String key, boolean value) {
        change(key, value);
        save();
    }

    /**
     * 保存用户信息
     *
     * @param jsonObject
     */
    public static void saveLoginInfo(JSONObject jsonObject) {
        try {
            Cfg.change("UserID", jsonObject.getString("UserID"));
            Cfg.change("SessionKey", jsonObject.getString("SessionKey"));
            Cfg.change("NickName", jsonObject.getString("Nickname"));
            Cfg.change("Icon", jsonObject.getString("Icon"));
            Cfg.change("LastName", jsonObject.getString("LastName"));
            Cfg.change("Sex", jsonObject.getString("Sex"));
            Cfg.change("BirthYear", jsonObject.getString("BirthYear"));
            Cfg.change("BirthMonth", jsonObject.getString("BirthMonth"));
            Cfg.change("Address", jsonObject.getString("Address"));
            Cfg.change("Addressen", jsonObject.getString("Addressen"));
            Cfg.change("Account", jsonObject.getString("Account"));
            Cfg.change("Email", jsonObject.getString("Email"));
            Cfg.change("AccountType",jsonObject.getString("AccountType"));
            Cfg.change("BannerUrl",jsonObject.getString("BannerUrl"));

            Cfg.save();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
